package servlet;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import thread.CopyThread;
import util.JdbcUtil;
/**
 * Joomla中创建Club时，用此servlet进行拷贝
 * @author GaoTong
 *
 */
public class Copy extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException{

		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		try{
			String srcFile = "C:/xampp/htdocs/Joomla/clubs/club01";
			String desFile = "C:/xampp/htdocs/Joomla/clubs/"+request.getParameter("club_world_name");
			File club01 = new File(srcFile);
			File clubNew = new File(desFile);
			club01.renameTo(clubNew);

			
			//开启新的线程  复制Joomla
			srcFile = "C:/xampp/htdocs/Joomla/clubs/club02";
			desFile = "C:/xampp/htdocs/Joomla/clubs/club01";
			CopyThread thread = new CopyThread(srcFile, desFile);
			thread.start();
			
			
			//修改配置文件
//			InputStream in = new FileInputStream(new File("C:\\xampp\\htdocs\\Joomla\\clubs\\configuration.php"));
//			int length = in.available();
//			byte buffer[] = new byte[length];
//			in.read(buffer);
//			String str = new String(buffer,"UTF-8");
//			in.close();
		
			//待修改
//			str = str.replace("#dbname#","club01");
//			str = str.replace("#dbname#",request.getParameter("club_world_name"));
			
			
//			System.out.println(str);
//			OutputStream os = new FileOutputStream(clubNew.getAbsolutePath()+"\\configuration.php",false);
//			DataOutputStream dos = new DataOutputStream(os);
//			dos.writeUTF(str);
//			System.out.println(clubNew.getAbsolutePath()+"\\configuration.php");
			
			
			//插入数据  可改用Hibernate
			Connection conn = JdbcUtil.getConnection("decuries");
			PreparedStatement st = conn.prepareStatement("insert into club(club_world_name,manger,club_local_name,country_code,province_code," +
					"language_code,motto,colour_code1,colour_code2,colour_code3,description,web_notes,small_graphic_icon) " +
					"values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
			System.out.println(request.getParameter("club_world_name"));
			st.setString(1, request.getParameter("club_world_name"));
			st.setInt(2, Integer.parseInt(request.getParameter("user_id")));
			st.setString(3,request.getParameter("club_local_name"));
			st.setString(4, request.getParameter("country"));
			st.setString(5, request.getParameter("province"));
			st.setString(6, request.getParameter("language"));
			st.setString(7, request.getParameter("motto"));
			st.setString(8, request.getParameter("color1").substring(1));
			st.setString(9, request.getParameter("color2").substring(1));
			st.setString(10, request.getParameter("color3").substring(1));
			st.setString(11, request.getParameter("description"));
			st.setString(12, request.getParameter("web_notes"));
			st.setString(13, request.getParameter("icon"));
			
			System.out.println(st.toString());
			st.execute();
			
//			dos.close();
//			os.close();
		}catch(Exception e){
			e.printStackTrace();
			try {
				response.sendRedirect("http://localhost/Joomla/j2regClubOk.php?result=fail");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		try {
			response.sendRedirect("http://localhost/Joomla/j2regClubOk.php?result=success&clubname="+request.getParameter("club_world_name"));
//			response.sendRedirect("http://localhost/Joomla/clubs/"+request.getParameter("club_world_name"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
}
